package com.heukyosuk.file;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.heukyosuk.main.R;
import com.heukyosuk.main.R.id;
import com.heukyosuk.main.R.layout;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class FileSearch extends Activity {

	ArrayList<FileClass> fileList;
	ListView listView;
	String root;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_file_list);

		listView = (ListView) findViewById(R.id.listView_search);
		fileList = new ArrayList<FileClass>();
		listView.setOnItemClickListener(new ListViewItemClickListener());

		root = Environment.getExternalStorageState();
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) findFolder();
		else Log.i("MAIN","Not find SDCard");
	}

	private void findFolder() {
		File files = new File(Environment.getExternalStorageDirectory().getPath() + "/heukyosuk");
		FileAdapter adapter = new FileAdapter(this, fileList);
		if(files.listFiles() != null) {
			if(files.listFiles().length > 0) {
				for(File file : files.listFiles()) {
					fileList.add(new FileClass(file.getName(), ""));
				}
			}
		} else
			finish();
		files = null;
		listView.setAdapter(adapter);
	}

	private class ListViewItemClickListener implements AdapterView.OnItemClickListener {
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			viewFile(getApplicationContext(), root + "/heukyosuk/", fileList.get(position).fileName);
		}
	}

	public static void viewFile(Context ctx, String filePath, String fileName) {
		Intent fileIntent = new Intent(Intent.ACTION_VIEW);
		fileIntent.addCategory(Intent.CATEGORY_DEFAULT);
		File file = new File(Environment.getExternalStorageDirectory().getPath() + "/heukyosuk/" + fileName);
		Uri uri = Uri.fromFile(file);
		String fileExtend = getExtension(file.getAbsolutePath());

		if (fileExtend.equalsIgnoreCase("mp3")) fileIntent.setDataAndType(uri, "audio/*");
		else if (fileExtend.equalsIgnoreCase("mp4")) fileIntent.setDataAndType(uri, "vidio/*");
		else if (fileExtend.equalsIgnoreCase("txt")) fileIntent.setDataAndType(uri, "text/*");
		else if (fileExtend.equalsIgnoreCase("pdf")) fileIntent.setDataAndType(uri, "application/pdf");
		else if (fileExtend.equalsIgnoreCase("hwp")) fileIntent.setDataAndType(uri, "application/haansofthwp");
		else if (fileExtend.equalsIgnoreCase("doc") || fileExtend.equalsIgnoreCase("docx")) fileIntent.setDataAndType(uri, "application/msword");
		else if (fileExtend.equalsIgnoreCase("xls") || fileExtend.equalsIgnoreCase("xlsx")) fileIntent.setDataAndType(uri, "application/vnd.ms-excel");
		else if (fileExtend.equalsIgnoreCase("ppt") || fileExtend.equalsIgnoreCase("pptx")) fileIntent.setDataAndType(uri, "application/vnd.ms-powerpoint");
		else if (fileExtend.equalsIgnoreCase("jpg") || fileExtend.equalsIgnoreCase("jpeg") || fileExtend.equalsIgnoreCase("gif") || fileExtend.equalsIgnoreCase("png") || fileExtend.equalsIgnoreCase("bmp")) fileIntent.setDataAndType(uri, "image/*");


		PackageManager pm = ctx.getPackageManager();
		List<ResolveInfo> list = pm.queryIntentActivities(fileIntent, PackageManager.GET_META_DATA);
		if (list.size() == 0) Toast.makeText(ctx, fileName + "�쓣 �솗�씤�븷 �닔 �엳�뒗 �빋�씠 �꽕移섎릺吏� �븡�븯�뒿�땲�떎.", Toast.LENGTH_SHORT).show();
		else {
			fileIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			ctx.startActivity(fileIntent);
		}
	}

	public static String getExtension(String fileStr) {
		return fileStr.substring(fileStr.lastIndexOf(".") + 1, fileStr.length());
	}

}
